﻿using LaoLuo.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace LaoLuo.Common.Test
{
    /// <summary>
    ///这是 IntervalTest 的测试类，旨在
    ///包含所有 IntervalTest 单元测试
    ///</summary>
    [TestClass()]
    public class IntervalTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        public void IsInClosedInterval<T>(T a, T b, T test, bool isIn = true)
            where T : IComparable<T>
        {
            var i = Interval<T>.NewClosedInterval(a, b);
            var actual = i.HasPoint(test);
            Assert.AreEqual(isIn, actual);
        }

        public void IsInOpenInterval<T>(T a, T b, T test, bool isIn = true)
            where T : IComparable<T>
        {
            var i = Interval<T>.NewOpenInterval(a, b);
            var actual = i.HasPoint(test);
            Assert.AreEqual(isIn, actual);
        }

        [TestMethod()]
        public void IntervalTestAll()
        {
            var now = DateTime.Now;

            var i = Interval<int>.NewOpenInterval(0, 100);
            Assert.AreEqual(i.BeginPoint, 0);
            Assert.AreEqual(i.EndPoint, 100);

            i = Interval<int>.NewLeftOpenRightClosedInterval(0, 100);
            Assert.AreEqual(i.BeginPoint, 0);
            Assert.AreEqual(i.EndPoint, 100);

            i = Interval<int>.NewLeftClosedRightOpenInterval(0, 100);
            Assert.AreEqual(i.BeginPoint, 0);
            Assert.AreEqual(i.EndPoint, 100);

            IsInClosedInterval(now.AddDays(-1), now.AddDays(1), now);
            IsInClosedInterval(new Compare(0), new Compare(100), new Compare(30));
            IsInClosedInterval(1, 100, 11);
            IsInClosedInterval(1, 100, 100);

            IsInOpenInterval(now.AddDays(-1), now.AddDays(1), now);
            IsInOpenInterval(new Compare(0), new Compare(100), new Compare(30));
            IsInOpenInterval(1, 100, 11);
            IsInOpenInterval(1, 100, 100, false);
        }

        public class Compare : IComparable<Compare>
        {
            public int Value { get; set; }

            public Compare(int value)
            {
                this.Value = value;
            }

            public int CompareTo(Compare other)
            {
                return this.Value.CompareTo(other.Value);
            }
        }
    }
}
